package org.mclll.xwangplugin.town.command.executor;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.mclll.xwangplugin.town.database.domain.TownApplyDO;
import org.mclll.xwangplugin.town.database.domain.TownDO;
import org.mclll.xwangplugin.town.database.domain.TownPlayerDO;
import org.mclll.xwangplugin.town.database.domain.TownShopDO;
import org.mclll.xwangplugin.town.database.mapper.manage.TownApplyManage;
import org.mclll.xwangplugin.town.database.mapper.manage.TownManage;
import org.mclll.xwangplugin.town.database.mapper.manage.TownPlayerManage;
import org.mclll.xwangplugin.town.database.mapper.manage.TownShopManage;
import org.mclll.xwangplugin.town.enums.PlayerTownIdentity;
import org.mclll.xwangplugin.town.lp.LuckPermsTown;

import java.util.List;
import java.util.stream.Collectors;

import static org.mclll.xwangplugin.town.config.TownContext.*;

/**
 * @author: geduo
 * @date: 2023/9/27 17:45
 */
public class DestroyTownExecutor {
    public static void destroy(CommandSender sender, String[] args) {
        // 移除城镇 - destroy <城镇名> [confirm]
        if (args.length < 2) {
            sender.sendMessage(DESTROY_TOWN_COMMAND_FORMAT);
            return;
        }
        if (args.length == 2) {
            // 二次确认
            sender.sendMessage(
                    Component.text(String.format(DESTROY_TOWN_CONFIRM, args[1]))
                            .appendNewline()
                            .append(
                                    Component.text(CONFIRM_BUTTON)
                                            .clickEvent(ClickEvent.runCommand(String.format("/poitown destroy %s confirm", args[1])))
                                            .hoverEvent(HoverEvent.showText(Component.text(CONFIRM_BUTTON)))
                            )
                            .append(Component.text("         "))
                            .append(Component.text(CANCEL_BUTTON).hoverEvent(HoverEvent.showText(Component.text(CANCEL_BUTTON))))
            );
            return;
        }
        if (args.length == 3 && "confirm".equals(args[2])) {
            String townName = args[1];
            TownDO selectParam = new TownDO();
            selectParam.setName(townName);
            List<TownDO> townList = TownManage.selectList(selectParam);
            if (townList.isEmpty()) {
                sender.sendMessage(TOWN_NOT_FOUND);
                return;
            }
            // 数据库删除城镇
            TownDO town = townList.get(0);
            town.setIsDelete(true);
            TownManage.updateByUuid(town);
            // 权限处理
            LuckPermsTown.deleteTown(town.getName());
            // 删除城镇玩家
            TownPlayerDO playerSelectParam = new TownPlayerDO();
            playerSelectParam.setTownUuid(town.getUuid());
            List<TownPlayerDO> townPlayerList = TownPlayerManage.selectList(playerSelectParam);
            if (!townPlayerList.isEmpty()) {
                // 权限处理
                LuckPermsTown.kickPlayerFromTown(townPlayerList.stream().map(TownPlayerDO::getName).collect(Collectors.toList()));
                TownPlayerDO updateParam = new TownPlayerDO();
                updateParam.setIdentity(PlayerTownIdentity.GENSHIN);
                updateParam.setTownUuid("-");
                TownPlayerManage.update(updateParam, playerSelectParam);
            }
            // 删除城镇申请数据
            TownApplyDO townApplyParam = new TownApplyDO();
            townApplyParam.setTownUuid(town.getUuid());
            TownApplyDO townApplyUpdate = new TownApplyDO();
            townApplyUpdate.setIsDelete(true);
            TownApplyManage.update(townApplyUpdate, townApplyParam);

            // 删除城镇商店
            TownShopDO townShopUpdate = new TownShopDO();
            townShopUpdate.setIsDelete(true);
            TownShopDO townShopParam = new TownShopDO();
            townShopParam.setTownUuid(town.getUuid());
            TownShopManage.update(townShopUpdate, townShopParam);
            // 删除成功
            sender.sendMessage(String.format(DESTROY_TOWN_SUCCESS, townName));
            for (int i = 0; i < 3; i++) {
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
                        "say " + String.format(DESTROY_TOWN_SUCCESS, townName)
                );
            }
        } else {
            sender.sendMessage(DESTROY_TOWN_COMMAND_FORMAT);
        }
    }

    public static List<String> townNameComplete(CommandSender sender, String[] args) {
        if (args.length == 2) {
            return TownManage.selectList(null).stream()
                    .map(TownDO::getName)
                    .filter(townName -> townName.startsWith(args[1]))
                    .collect(java.util.stream.Collectors.toList());
        }
        return null;
    }
}
